/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cics_report_msg_data_resp.c                      */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/23 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frct_comm.h"

#include <cics/config.h>
#include <cics/cics_core.h>
#include <cics/cics_comm.h>
#include <cics/cics_packet.h>
#include <apmtest_comm.h>
#include <frreport_opt.h>
#include "../test_cics_comm.h"

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    Frreport_body_data *p_st_body = NULL;
    L7_cics_stmt *p_st_stmt = NULL;
    Frlist_node *p_st_node = NULL;

    CU_ASSERT_EQUAL(test_cics_create_ct(&p_st_conn, TEST_CICS_SRC_IP, TEST_CICS_DST_IP, TEST_CICS_SRC_PORT, TEST_CICS_DST_PORT), FR_SUCC);

    L7_session *p_st_session = frct_get_session(p_st_conn);

    L7session_cics_private *p_st_session_private = (L7session_cics_private *) frct_get_session_private(p_st_session);
    L7session_cics *p_st_sess = p_st_session_private->p_st_sess;
    L7_cics_packet *p_st_cics_packet = p_st_session_private->p_st_cics_packet;

    char *p_bin = (char *) "47617465007010000000000300000000000000000000000345434900000005460000000c00000000000000000000000020202020000020000101000005273c3f786d6c2076657273696f6e3d22312e30223f3e3c54726164653e3c526571756573744865616465723e3c627573696e65737341636365707449643e5a46313531303239353034393135363c2f627573696e65737341636365707449643e3c73657269616c4e756d6265723e303c2f73657269616c4e756d6265723e3c76657273696f6e3e312e303c2f76657273696f6e3e3c636d64547970653e303c2f636d64547970653e3c7265717565737465723e434e415030313c2f7265717565737465723e3c6368616e6e656c3e435a42303c2f6368616e6e656c3e3c7265717565737454696d655374616d703e323031352d31302d32392031353a33383a30382e3030303330313c2f7265717565737454696d655374616d703e3c6f7267616e697a6174696f6e556e697449643e3333313030303030303c2f6f7267616e697a6174696f6e556e697449643e3c74656c6c6572313e7a666d7a3c2f74656c6c6572313e3c74656c6c6572323e3c2f74656c6c6572323e3c7365727669636549643e636c434e41504c5a3c2f7365727669636549643e3c2f526571756573744865616465723e3c426f64793e3c70656e64696e674167656e743e3333313030303030303c2f70656e64696e674167656e743e3c70617965724163636f756e743e37383635363435363739303432333c2f70617965724163636f756e743e3c70617965724e616d653ecdf5c0cfbcaa3c2f70617965724e616d653e3c70617965654163636f756e743e393030303030313439303c2f70617965654163636f756e743e3c70617965654e616d653ecdf5c0cfbcaa3c2f70617965654e616d653e3c616d743e35353530303030302e30303c2f616d743e3c6163636f756e74547970653e3030303c2f6163636f756e74547970653e3c6163636f756e74696e675375626a65637443617465676f72793e313c2f6163636f756e74696e675375626a65637443617465676f72793e3c6261636b466c61673e313c2f6261636b466c61673e3c6261636b6669656c643eb1b8d7a23c2f6261636b6669656c643e3c6d736769643e5a46313531303239353034393135363c2f6d736769643e3c72657365727665313e3c2f72657365727665313e3c72657365727665323e3c2f72657365727665323e3c72657365727665333e3c2f72657365727665333e3c66696e616c70656e64696e676167656e743e3333313030303030303c2f66696e616c70656e64696e676167656e743e3c70656e64696e67636f64653e4b3c2f70656e64696e67636f64653e3c70656e64696e67646573633ecdf8c9cfb4e6bfeec7bfd6c6b9d2d5cb3c2f70656e64696e67646573633e3c616363746f72673e3333313030303030303c2f616363746f72673e3c6163636f756e74726573756c743e4b3c2f6163636f756e74726573756c743e3c7472616e73616374696f6e73657269616c6e6f3e38313134353734353c2f7472616e73616374696f6e73657269616c6e6f3e3c2f426f64793e3c526573706f6e73654865616465723e3c686f737453657269616c4e6f3e38313134353734353c2f686f737453657269616c4e6f3e3c726573706f6e736554696d655374616d703e323031352d31302d32393c2f726573706f6e736554696d655374616d703e3c6572726f724e6f3e3030303030303c2f6572726f724e6f3e3c6572726f72496e666f3eb6feb4fab4f3b6eec0b4d5cabcc7d5cbb3c9b9a63c2f6572726f72496e666f3e3c2f526573706f6e73654865616465723e3c736f614d6f7a3e3c6d6f7a78783e3c2f6d6f7a78783e3c2f736f614d6f7a3e3c2f54726164653e00";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_CICS_SRC_IP, TEST_CICS_DST_IP, TEST_CICS_SRC_PORT, TEST_CICS_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    CU_ASSERT_EQUAL(cics_packet_unfragment(p_st_sess, p_st_packet, p_st_cics_packet), APM_CICS_CHECK_FRAGMENT_SUCCESS);

    Frhash_header st_hash_report;
    CU_ASSERT_EQUAL(frhash2_create(&st_hash_report, 2), FR_SUCC);

    p_st_session_private->p_report_comm = frlist_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_session_private->p_report_comm);
    p_st_session_private->p_report_req = frlist_create();
    CU_ASSERT_PTR_NOT_NULL(p_st_session_private->p_report_req);

    p_st_sess->p_encoding = frstrdup("gb18030");

    p_st_stmt = (L7_cics_stmt *) frmalloc(sizeof(L7_cics_stmt));
    CU_ASSERT_PTR_NOT_NULL(p_st_stmt);
    memset(p_st_stmt, 0x00, sizeof(L7_cics_stmt));
    p_st_stmt->ui_msg_id = p_st_cics_packet->ui_msg_id;
    p_st_stmt->ui_extend_mode = 0;
    p_st_stmt->i_b_callback_exists = 0;

    p_st_node = frlist_node_create(p_st_stmt);
    CU_ASSERT_EQUAL(frhash2_addLong(p_st_session_private->p_hash_stmt, p_st_cics_packet->ui_msg_id, p_st_node), FR_SUCC);

    p_st_body = frreport_body_new_l7(p_st_conn);
    CU_ASSERT_PTR_NOT_NULL(p_st_body);

    CU_ASSERT_EQUAL(frreport_body_l7_add_str(p_st_body, APM_REPORT_KEY_TRANS_TITLE, "test"), FR_SUCC);

    CU_ASSERT_EQUAL(cics_report_msg_data_resp(p_st_conn, p_st_sess, p_st_cics_packet, p_st_body, &st_hash_report), FR_SUCC);

    Frvar_field *p_st_field = NULL;
    unsigned int ui_loop = 0;
    Frlist_header *p_list_report = NULL;
    Frlist_node *p_st_node_report = NULL;
    Frreport_data *p_st_report_data = NULL;
    FRHASH_LOOP(ui_loop, &st_hash_report, p_list_report)
    {
        if (p_list_report == NULL)
            continue;
        p_st_node_report = FRLIST_FIRST_NODE(p_list_report);
        while (p_st_node_report)
        {
            p_st_report_data = (Frreport_data *) FRLIST_GET_NODE_DATA(p_st_node_report);
            if (p_st_report_data)
            {
                p_st_field = FrReportOpt_get_field(p_st_report_data, APM_REPORT_KEY_TRANS_TITLE, 0);
                CU_ASSERT_EQUAL(p_st_field->em_data_type, FRSTRING);
                CU_ASSERT_STRING_EQUAL(p_st_field->p_field_name, "transaction.title.s");
                CU_ASSERT_STRING_EQUAL(p_st_field->field_value.p_value, "test");
            }
            p_st_node_report = FRLIST_NEXT_NODE(p_st_node_report);
        }
    }

    frhash2_free(&st_hash_report, list_report_node_free);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    test_cics_destroy_ct(&p_st_conn);
    CU_ASSERT_PTR_NULL(p_st_conn);
}

int add_suite_cics_report_msg_data_resp(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cics_report_msg_data_resp()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
